﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Arcanea.StateEditor.XmlObjects;

namespace Arcanea.StateEditor.FormOption
{
    public partial class FormOptionGenerate : Form
    {
        public List<BulleTexteXml> allBulles = new List<BulleTexteXml>();
        public List<LinkRepresentationXml> allLinks = new List<LinkRepresentationXml>();

        public FormOptionGenerate()
        {
            InitializeComponent();
        }
        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            FormPrintGenerate fpg = new FormPrintGenerate();
            fpg.Texte = Generate();
            fpg.ShowDialog();
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private string Generate()
        {
            //Génération de l'enum
            string result = "enum states { " + Environment.NewLine;
            foreach (BulleTexteXml btx in allBulles)
            {
                result += btx.Texte + "," + Environment.NewLine;
            }
            result += "}" + Environment.NewLine + Environment.NewLine;

            //Génération de la machine à etat

            result += "public static void ReceivePacket(ref states actualState, ref XmlPacket packet, ref List<XmlPacket> buffer)" + Environment.NewLine;
            result += "{" + Environment.NewLine;
            result += "    switch(actualState)" + Environment.NewLine;
            result += "    {" + Environment.NewLine;

            foreach (BulleTexteXml btx in allBulles)
            {
                result += "        case " + btx.Texte + " :" + Environment.NewLine;

                foreach (LinkRepresentationXml link in allLinks)
                {
                    string ifStatement = string.Empty;

                    foreach (int linkGUID in btx.GUIDLinks)
                    {
                        //Cas ou c'est un fils du link
                        if (linkGUID == link.GUID && link.GUIDFils == btx.GUID)
                        {
                            string ifCondition = string.Empty;
                            foreach (string packet in link.AllPacketAdmited)
                            {
                                if (ifCondition.Length != 0)
                                {
                                    ifCondition += " || packet.Name == \"" + packet + "\"";
                                }
                                else
                                {
                                    ifCondition += "packet.Name == \"" + packet + "\"";
                                }
                                //Ajouter un if ou un elseif
                                if (ifStatement != string.Empty)
                                {
                                    ifStatement += "            else if(" + ifCondition + ")" + Environment.NewLine;
                                }
                                else
                                {
                                    ifStatement += "            if(" + ifCondition + ")" + Environment.NewLine;
                                }
                                ifStatement += "            {" + Environment.NewLine;
                                if (link.IsClientDeclenche)
                                {
                                    ifStatement += "                // Déclenché par l'envoie du client" + Environment.NewLine;
                                }
                                else
                                {
                                    ifStatement += "                // Déclenché par l'envoie du serveur" + Environment.NewLine;
                                }
                                ifStatement += "                " + btx.Texte + "_" + packet + "(ref actualState, ref packet, ref buffer);" + Environment.NewLine;
                                ifStatement += "            }" + Environment.NewLine;
                            }
                        }
                    }
                    if (ifStatement != string.Empty)
                    {
                        ifStatement += "            else" + Environment.NewLine;
                        ifStatement += "            {" + Environment.NewLine;
                        ifStatement += "                throw new Exception(\"Reception of \" + packet.Name + \" in state " + btx.Texte + "\");" + Environment.NewLine;
                        ifStatement += "            }" + Environment.NewLine;
                    }
                    result += ifStatement;
                }

                result += "            break; " + Environment.NewLine;
            }
            result += "    }" + Environment.NewLine;
            result += "}" + Environment.NewLine;

            return result;
        }


    }
}
